package cn.itbeien.payment.controller;

import cn.itbeien.payment.service.IPaymentService;
import cn.itbeien.response.CommonResponse;
import cn.itbeien.vo.PaymentVO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.itbeien.vo.CallBackVO;

/**
 * @author beien
 * @date 2024-03-18 15:14
 * Copyright© 2024 beien
 */
@RestController
@RequestMapping("pay")
@Slf4j
public class PaymentController {

    @Autowired
    private IPaymentService paymentService;

    @PostMapping("doPay.do")
    public CommonResponse doPay(@RequestBody JSONObject jsonObject){
        CommonResponse commonResponse = new CommonResponse();
        String result  = "fail";
        //jsonObject转PaymentVO
        PaymentVO paymentVO = jsonObject.toJavaObject(PaymentVO.class);

        try{
            //1.相关参数的交易，签名(RSA(公钥和私钥),MD5)
            //2.调用聚合支付service(添加事务管理)
            commonResponse =   paymentService.doService(paymentVO);

        }catch (Exception e){
            log.error("支付交易异常入口方法异常:{}",e);
            commonResponse.setCode("500");
            commonResponse.setMessage("系统异常");
        }
        return commonResponse;
    }

    @PostMapping("callback")//nacos中的callback url和这里保持一致
    public  CallBackVO callBack(@RequestBody String data){
        CallBackVO  callBackVO = new CallBackVO();
        try{
            //解析上游传入的json数据

        }catch (Exception e){
            log.error("支付回调系统异常:{}",e);
        }

        return callBackVO;
    }
}
